#include <my_head.h>

int main(int argc, const char *argv[]){
    if(3 != argc){
        printf("Usage : %s file_server file_client\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    // 创建套接字
    int sockfd = 0;
    if(-1 == (sockfd = socket(AF_UNIX, SOCK_STREAM, 0)))
        ERR_LOG("scoket error");
    // 填充服务器本地结构体
    struct sockaddr_un serveraddr;
    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, argv[1]);
    socklen_t serveraddr_len = sizeof(serveraddr);
    // 与服务器建立连接
    if(-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len))
        ERR_LOG("connect error");
    printf("与服务器连接成功..\n");

    // 填充客户端本地结构体
    struct sockaddr_un clientaddr;
    clientaddr.sun_family = AF_UNIX;
    strcpy(clientaddr.sun_path, argv[2]);
    socklen_t clientaddr_len = sizeof(clientaddr);
    // 绑定客户端自己的结构体
    if(-1 == bind(sockfd, (struct sockaddr *)&clientaddr, clientaddr_len))
        ERR_LOG("bind error");

    // 处理读写
    char buff[128] = {0};
    while(1){
        fgets(buff, sizeof(buff), stdin);
        buff[strlen(buff)-1] = '\0';
        // 给服务器发送消息
        if(-1 == send(sockfd, buff, sizeof(buff), 0))
            ERR_LOG("send error");
        if(!strcmp(buff, "quit")){
            printf("客户端退出了..\n");
            break;
        }
        // 接收服务器发来的应答
        if(-1 == recv(sockfd, buff, sizeof(buff), 0))
            ERR_LOG("recv error");
        printf("服务器回复的消息是[%s]\n", buff);
    }
    // 关闭套接字
    close(sockfd);

    return 0;
}